package com.qmx.launcher.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.ContentObserver;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.format.DateFormat;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.DigitalClock;
import android.widget.TextView;

import java.util.Calendar;

/**
 * @author chengxiangtong
 * @date 2018/11/14
 * version : 1.0
 * class describe：*
 */
@SuppressLint("AppCompatCustomView")
public class CustomerDigitalClock extends TextView {
    // FIXME: implement separate views for hours/minutes/seconds, so
    // proportional fonts don't shake rendering

    Calendar mCalendar;
    private static final int msgKey = 1;

    public CustomerDigitalClock(Context context) {
        super(context);
        initClock();
    }

    public CustomerDigitalClock(Context context, AttributeSet attrs) {
        super(context, attrs);
        initClock();
    }

    private void initClock() {
        if (mCalendar == null) {
            mCalendar = Calendar.getInstance();
        }
        new TimeThread().start();
    }

    public class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = msgKey;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case msgKey:
                    long sysTime = System.currentTimeMillis();
                    mCalendar.setTimeInMillis(sysTime);
                    //                    CharSequence sysTimeStr = mCalendar.get(Calendar.HOUR) + ":" + mCalendar.get(Calendar.MINUTE);
                    setText(formatTime(mCalendar.get(Calendar.HOUR), mCalendar.get(Calendar.MINUTE)));
                    break;
                default:
                    break;
            }
        }
    };

    public SpannableString formatTime(int hour, int minute) {
        StringBuffer buffer = new StringBuffer();
        buffer.append((hour < 10 ? "0" + hour : hour + ""));
        buffer.append(":");
        buffer.append((minute < 10 ? "0" + minute : minute + ""));
        buffer.append(" ");//1个空格

        if (mCalendar.get(Calendar.AM_PM) == 0) {
            //白天
            buffer.append("am");
        } else {
            //晚上
            buffer.append("pm");
        }
        SpannableString spannableString = new SpannableString(buffer);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#42ADFF"));
        spannableString.setSpan(new AbsoluteSizeSpan(24), 6, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(colorSpan, 6, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

}
